package io.renren.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.common.service.impl.CrudServiceImpl;
import io.renren.dao.UnionidmDao;
import io.renren.dto.UnionidmDTO;
import io.renren.dto.WeChatMiniAuthorizeVo;
import io.renren.entity.UnionidmEntity;
import io.renren.service.UnionidmService;
import cn.hutool.core.util.StrUtil;
import io.renren.utils.WechatMsg;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Map;
import java.util.UUID;

/**
 * ${comments}
 *
 * @author Sen 524227263@qq.com
 * @since 1.0.0 2024-06-10
 */
@Service
public class UnionidmServiceImpl extends CrudServiceImpl<UnionidmDao, UnionidmEntity, UnionidmDTO> implements UnionidmService {

    @Override
    public QueryWrapper<UnionidmEntity> getWrapper(Map<String, Object> params) {
        String id = (String) params.get("id");

        QueryWrapper<UnionidmEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StrUtil.isNotBlank(id), "id", id);

        return wrapper;
    }


    @Override
    public UnionidmEntity findOneByOpenId(String openId, String type) {
        UnionidmEntity unionidmEntity = baseDao.selectOne(new QueryWrapper<UnionidmEntity>()
                .eq("Type", type)
                .eq("Open_ID", openId));
        if (unionidmEntity != null) {
            return unionidmEntity;
        }
        return null;
    }

    @Override
    public UnionidmEntity saveInfo(String openId) {
        UnionidmEntity unionidmEntity = new UnionidmEntity();
        try {
            String token = WechatMsg.getToken();
            String unionid = WechatMsg.getWxUnionidByWxOpenid(token, openId);
            if (StrUtil.isNotBlank(unionid)) {
                unionidmEntity.setUnionmid(UUID.randomUUID().toString());
                unionidmEntity.setOpenId(openId);
                unionidmEntity.setUnionId(unionid);
                unionidmEntity.setType("1");
                baseDao.insert(unionidmEntity);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return unionidmEntity;
    }

    @Override
    public UnionidmEntity saveInfoMini(WeChatMiniAuthorizeVo weChatAccessPhoneNumberVo) {
        UnionidmEntity unionidmEntity = new UnionidmEntity();
        unionidmEntity.setUnionmid(UUID.randomUUID().toString());
        unionidmEntity.setOpenId(weChatAccessPhoneNumberVo.getOpenId());
        unionidmEntity.setUnionId(weChatAccessPhoneNumberVo.getUnionId());
        unionidmEntity.setType("2");
        baseDao.insert(unionidmEntity);
        return unionidmEntity;
    }

    @Override
    public UnionidmEntity findOneByUnionId(String unionId, String type) {
        UnionidmEntity unionidmEntity = baseDao.selectOne(new QueryWrapper<UnionidmEntity>()
                .eq("Type", type)
                .eq("Union_ID", unionId));
        if (unionidmEntity != null) {
            return unionidmEntity;
        }
        return null;
    }
}